{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import math\n",
    "import random\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../img/02/300.jpg'\n",
    "img = cv2.imread(path, cv2.IMREAD_COLOR) #读入\n",
    "\"\"\" cv2.imshow(\"test\", img)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "(b, g, r) = cv2.split(img) #通道分离\n",
    "\"\"\" cv2.imshow(\"test\", b)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() \"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "rat, scr = cv2.threshold(b, 100, 255, cv2.THRESH_BINARY) #二值化\n",
    "\"\"\" cv2.imshow(\"test\", scr)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,5)) #腐蚀、膨胀核\n",
    "opened = cv2.morphologyEx(scr, cv2.MORPH_OPEN, kernel) #开操作\n",
    "closed = cv2.morphologyEx(opened, cv2.MORPH_CLOSE, kernel) #闭操作\n",
    "\"\"\" cv2.imshow(\"test\", closed)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() \"\"\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "lim_dir_area = 10\n",
    "lim_pro_area = 0.6\n",
    "lim_pro = 0.2\n",
    "\n",
    "lights = []\n",
    "\n",
    "temp = np.ones(img.shape,np.uint8)*255\n",
    "contours, hierarchy=cv2.findContours(closed, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) #边缘检测\n",
    "for contour in contours:\n",
    "    lightArea = cv2.contourArea(contour)\n",
    "    if lightArea <= lim_dir_area:\n",
    "        continue\n",
    "\n",
    "    lightRec = cv2.fitEllipse(contour)\n",
    "    RecArea = lightRec[1][0] * lightRec[1][1]\n",
    "\n",
    "    if lightArea / RecArea <= lim_pro_area:\n",
    "        continue\n",
    "\n",
    "    if lightRec[1][0]/lightRec[1][1] >= lim_pro:\n",
    "        continue\n",
    "\n",
    "    lights.append(lightRec)\n",
    "    cv2.ellipse(temp,lightRec,(0,255,0),1)\n",
    "\n",
    "print(lights)\n",
    "\n",
    "#cv2.drawContours(temp, contours, -1, (0, 0, 255),1)\n",
    "\"\"\" cv2.imshow(\"test\", temp)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows() \"\"\"\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def takeX(L):\n",
    "    return  L[0][0] \n",
    "lights.sort(key=takeX)\n",
    "print(lights)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "l = len(lights)\n",
    "lim_angle = 5\n",
    "lim_temp_angle = 30\n",
    "\n",
    "\n",
    "boards = []\n",
    "for i in range(l):\n",
    "    for j in range(i+1,l):\n",
    "        dAngle = abs(lights[i][2] - lights[j][2])\n",
    "        dx = lights[j][0][0] - lights[i][0][0]\n",
    "        dy = lights[j][0][1] - lights[i][0][1]\n",
    "\n",
    "\n",
    "\n",
    "        temp = math.atan(dy/dx) * 180/math.pi\n",
    "        if lights[i][2] > 90:\n",
    "            dif_angle = abs(lights[i][2] - 180 + temp)\n",
    "        else:\n",
    "            dif_angle = abs(lights[i][2] + temp)\n",
    "        print(dif_angle)\n",
    "\n",
    "        if dAngle < lim_angle and dif_angle < lim_temp_angle:\n",
    "            boards.append((i,j))\n",
    "            break\n",
    "\n",
    "print(boards)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "def get_ellipse_param(major_radius, minor_radius, angle):\n",
    "    a, b = major_radius, minor_radius\n",
    "    sin_theta = np.sin(-angle)\n",
    "    cos_theta = np.cos(-angle)\n",
    "    A = a**2 * sin_theta**2 + b**2 * cos_theta**2\n",
    "    B = 2 * (a**2 - b**2) * sin_theta * cos_theta\n",
    "    C = a**2 * cos_theta**2 + b**2 * sin_theta**2\n",
    "    F = -a**2 * b**2\n",
    "    return A, B, C, F\n",
    "\n",
    "\n",
    "def calculate_rectangle(A, B, C, F):\n",
    "    '''\n",
    "    椭圆上下外接点的纵坐标值\n",
    "    '''\n",
    "    y = np.sqrt(4*A*F / (B**2 - 4*A*C))\n",
    "    y1, y2 = -np.abs(y), np.abs(y)\n",
    "    \n",
    "    '''\n",
    "    椭圆左右外接点的横坐标值\n",
    "    '''\n",
    "    x = np.sqrt(4*C*F / (B**2 - 4*C*A))\n",
    "    x1, x2 = -np.abs(x), np.abs(x)\n",
    "    \n",
    "    return (x1, y1), (x2, y2)\n",
    "\n",
    "def get_rectangle(major_radius, minor_radius, angle, center_x, center_y):\n",
    "    A, B, C, F = get_ellipse_param(major_radius, minor_radius, angle)\n",
    "    p1, p2 = calculate_rectangle(A, B, C, F)\n",
    "    return (center_x+p1[0], center_y+p1[1]), (center_x+p2[0], center_y+p2[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "for board in boards:\n",
    "    a = lights[board[0]]\n",
    "    b = lights[board[1]]\n",
    "\n",
    "    p1,p2 = get_rectangle(a[1][0],a[1][1],a[2] * np.pi /180 ,a[0][0],a[0][1])\n",
    "    p3,p4 = get_rectangle(b[1][0],b[1][1],b[2] * np.pi /180 ,b[0][0],b[0][1])\n",
    "\n",
    "    min_x = int(min(p1[0],p2[0],p3[0],p4[0]))\n",
    "    min_y = int(min(p1[1],p2[1],p3[1],p4[1]))\n",
    "    max_x = int(max(p1[0],p2[0],p3[0],p4[0]))\n",
    "    max_y = int(max(p1[1],p2[1],p3[1],p4[1]))\n",
    "    #print(min_x,min_y,max_x,max_y)\n",
    "\n",
    "    cv2.rectangle(img,(min_x, max_y),(max_x, min_y),(255,0,0),1)\n",
    "\n",
    "\n",
    "cv2.imshow(\"test\", img)\n",
    "cv2.waitKey()\n",
    "cv2.destroyAllWindows()\n",
    "    \n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np\n",
    "import math\n",
    "import random\n",
    "import matplotlib.pyplot as plt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "path = '../img/02/100.jpg'\n",
    "img = cv2.imread(path, cv2.IMREAD_COLOR) \n",
    "for i in range(500):\n",
    "    ran_x = random.randint(600,680)\n",
    "    ran_y = random.randint(600,1080)\n",
    "    img = cv2.imread(path, cv2.IMREAD_COLOR) \n",
    "    cv2.rectangle(img,(ran_x, ran_y),(ran_x+64, ran_y+64),(255,0,0),1)\n",
    "    croped_region = img[ran_x:ran_x+64,ran_y:ran_y+64]\n",
    "        #cv2.rectangle(img,(min_x, max_y),(max_x, min_y),(255,0,0),1)\n",
    "    cv2.imwrite('../img/pre_train/0_2/%d.jpg'%i,croped_region)\n",
    "\n",
    "# cv2.imshow(\"test\", img)\n",
    "# cv2.waitKey()\n",
    "# cv2.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "37d17ca93e76f3a19498fbec37b78c0588f5b59cd695a490507628cc801ea1e0"
  },
  "kernelspec": {
   "display_name": "Python 3.5.6 64-bit ('cv': conda)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.11"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
